import { login, logout, autoLogin } from "api/api"
import router from "router/router"
import { blackRoutes } from "@/util/blackRoute"
export default {
    state: {
        token: localStorage.getItem("sph_token"),
        userInfo: {}  //userInfo是否是一个空对象 代表了用户是否处于登录状态!!!
    },
    getters: {

    },
    mutations: {
        getToken(state, token) {
            state.token = token
        },
        getUserInfo(state, userInfo) {
            state.userInfo = userInfo
        }
    },
    actions: {

        //登录
        async login(store, { phone, password, redirectUrl }) {
            try {
                const { code, data, message } = await login({ phone, password });
                if (code === 200) {
                    //将登录放回的信息入vuex仓库
                    store.commit("getToken", data.token)
                    store.commit("getUserInfo", {
                        name: data.name,
                        userId: data.userId,
                    })
                    //持久化token
                    localStorage.setItem("sph_token", data.token)
                    //在哪一页登录 成功之后就去哪一页
                    await router.push(redirectUrl)
                } else {
                    //打印错误信息
                    alert(message)
                }
                return { code }
            } catch (e) {
                return Promise.reject(e)
            }
        },
        async loginOut(store) {
            try {
                //发请求，让token失效，删除仓库中用户信息
                const { code, message } = await logout();
                if (code === 200) {
                    store.commit('getToken', '')
                    store.commit('getUserInfo', {})
                    localStorage.removeItem('sph_token')
                    //在所有可以在未登录情况下访问的页面退出登录 成功之后留在该页面
                    let currentRouteName = router.currentRoute.name
                    let rootName = currentRouteName
                    // console.log(rootName)
                    if (blackRoutes.includes(rootName)) {
                        await router.push('/login')
                    } else {
                        if (rootName === 'cart') {
                            await store.dispatch('getCartList')
                        }
                    }
                } else {
                    alert(message)
                }
                return { code }
            } catch (e) {
                return Promise.reject(e)
            }

        },
        //自动登录
        async autoLogin(store) {
            try {
                const { code, data } = await autoLogin();
                if (code === 200) {
                    //自动登录成功  不会延长token的时效!
                    //需要把userInfo重置
                    store.commit("getUserInfo", {
                        name: data.name,
                        userId: data.id,
                    })
                } else {
                    //如果自动登录失败  token失效! 直接走退出登录逻辑
                    await store.dispatch("loginOut")
                }
                return { code }
            } catch (e) {
                return Promise.reject(e)
            }
        },
    }
}
